package messenger.svlt;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import messenger.domain.ChatSession;
import messenger.domain.Message;
import messenger.domain.User;
import net.sf.json.JSONArray;

/**
 * Servlet implementation class MessageControl
 */
public class MessageControl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static ChatSession chatSession;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageControl() {
    	chatSession = new ChatSession();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String method = request.getParameter("method");
			
		if ("index".equals(method)) {
			index(request, response);
		} else if ("chat".equals(method)) {
			chat(request, response);
		} else if ("send".equals(method)) {
			send(request, response);
		} else if ("check".equals(method)) {
			check(request, response);
		} else if ("clear".equals(method)) {
			clear(request, response);
		} else {
			index(request, response);
		}
	}
 
	private void index(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		String userName = request.getParameter("userName");
		
		if (User.isAutorizedUser(request.getSession())) {
			chat(request, response);
		} else if (userName != null && !"".equals(userName.trim())) {
			User user = new User();
			user.setName(userName);
			user.setChatSession(chatSession);
			User.addUserToSession(request.getSession(), user);
			chatSession.addUser(user);
			chat(request, response);
		} else {
			getServletContext().getRequestDispatcher("/jsp/messenger/index.jsp").forward(request, response);
		}
	}
	
	private void chat(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		getServletContext().getRequestDispatcher("/jsp/messenger/chat.jsp").forward(request, response);
	}	
	
	private void send(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		String message = request.getParameter("message");
		if (message != null) {
			User user = User.getUserFromSession(request.getSession());
			chatSession.notifyMessage(message, user);
		}
	}		
	
	private void check(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		// TODO - need to recovery logic
		Message[] messageArray = User.getUserFromSession(request.getSession()).consumeMessages();
		
		if (messageArray != null && messageArray.length > 0) {
			response.setContentType("application/json; charset=UTF-8");
			
			JSONArray jsonArray = JSONArray.fromObject(messageArray);
			
			System.out.println(jsonArray.toString());
			
			response.getWriter().print(jsonArray.toString());
		}
		response.getWriter().flush();
	}	
	
	private void clear(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
		
	}
}
